package cn.edu.neusoft.yuejian4.yuejian_4.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.adapter.MyLikeClubAdapter;
import cn.edu.neusoft.yuejian4.yuejian_4.adapter.ShowListAdapter;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Slideshow;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Success;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.List_TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.ClubModel;
import cn.edu.neusoft.yuejian4.yuejian_4.model.MyModel;


/**
 * Created by Lix on 2018/10/24.
 */

public class MyShowActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView back;
    private RecyclerView recyclerView;
    private SharedPreferences sp;
    private int uid;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_show);
        initView();
        initEvent();
        sp = getSharedPreferences("userinfo",MODE_PRIVATE);
        uid = sp.getInt("uid",4);
        MyModel myModel = new MyModel();
        myModel.myShow(uid,myShowListener);
    }

    List_TListener<Slideshow> myShowListener = new List_TListener<Slideshow>() {
        @Override
        public void onResponse(final List<Slideshow> list) {
            ShowListAdapter showListAdapter = new ShowListAdapter(getApplicationContext(),list);
            recyclerView.setAdapter(showListAdapter);
            showListAdapter.setmOnItemClickListener(new ShowListAdapter.mOnItemClickListener() {
                @Override
                public void onClick(View view, final int position) {
                    switch (view.getId()){
                        case R.id.delete_my_show:
                            AlertDialog.Builder builder = new AlertDialog.Builder(MyShowActivity.this);
                            builder.setTitle("删除-晒图");
                            builder.setMessage("你确定删除?");
                            builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    MyModel myModel = new MyModel();
                                    myModel.delMyShow(list.get(position).getId(), new TListener<Success>() {
                                        @Override
                                        public void onResponse(Success success) {
                                            Toast.makeText(MyShowActivity.this,"删除成功", Toast.LENGTH_SHORT).show();
                                        }

                                        @Override
                                        public void onFail(String msg) {

                                        }
                                    });

                                }
                            });
                            builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            });
                            AlertDialog dialog = builder.create();
                            dialog.show();
                            break;
                    }
                }
            });
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private void initView(){
        back = (ImageView)findViewById(R.id.my_show_back);
        recyclerView = (RecyclerView)findViewById(R.id.rv_my_show);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swiper_my_show);
        swipeRefreshLayout.setColorSchemeColors(Color.RED, Color.BLUE);

    }
    private void initEvent(){
        back.setOnClickListener(this);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                MyModel myModel = new MyModel();
                myModel.myShow(uid,myShowListener);
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.my_show_back:
                finish();
                break;
        }
    }
}
